<template>
  <div v-if="playlist">
    <div class="topheader">
      <div class="img">
        <img :src="playlist.coverImgUrl" alt="" />
      </div>
      <div class="title">
        {{ playlist.name }}
      </div>
      <div class="button" @click="$router.go(-1)">
        <span class="iconfont icon-fanhui"></span>
        {{ playlist.tags[0] || "排行榜" }}
      </div>
    </div>

    <dl class="musicList">
      <dt>
        <i @click="PlaylisAlls" class="iconfont icon-24gf-play"></i>播放全部<span
          >(共{{ playlist.tracks.length }}首)</span
        >
         <span
            class="texticon"
            @click="dianzis"
            :class="['iconfont', dianzan ?'icon-shixin': 'icon-kongxin']"
          > 
          </span>
      </dt>
      <dd
        v-for="(item, index) in playlist.tracks"
        :key="item.id"
        @click="setId(item.id)"
      >
        <h1>{{ index + 1 }}</h1>
        <div class="info">
          <h2>{{ item.name }}</h2>
          <h3>
            <span v-for="value in item.ar" :key="value.id"
              >{{ value.name }}&nbsp;</span
            >
          </h3>
        </div>
      </dd>
    </dl>
  </div>
</template>

<script>
import { getPlaylistDetail } from "../../api/rank";
import { Toast } from "vant";
import {
  getPlaylistSubscribe,
} from "../../api/base";
export default {
  data() {
    return {
      playlist: null,
      PlaylisAll:null,
      playerLists:null,
      dianzan: null, //收藏歌单
    };
  },
  created() {
    let queryId = this.$route.query;
    if (!("id" in queryId)) {
      this.$router.push("/rank");
    }
    getPlaylistDetail({ id: queryId.id }).then((data) => {
      console.log(data);
      this.playlist = data.playlist;
      this.playerLists = data.playlist.tracks; //获取歌单数据
      console.log(data.playlist.subscribed);
      this.dianzan = data.playlist.subscribed
    });
  },
  methods: {
    setId(id) {
      this.$emit("get-play-all-id",this.$route.query.id);//专递歌单id
      this.$emit("get-player-id", id);
      console.log(this.$route.query.id)
    },
    PlaylisAlls(){
      this.PlaylisAll = this.$route.query.id;
      getPlaylistDetail({ id: this.PlaylisAll }).then((data) => {
       this.playerLists = data.playlist.tracks; //获取歌单数据
       this.$emit("get-play-all-id",this.PlaylisAll);//专递歌单id
       this.$emit("get-player-id", this.playerLists[0].id);
    });
    },
    // 收藏部分
    dianzis() {
      this.dianzan = !this.dianzan;
      if (!this.dianzan) {
        Toast({
          message: "取消收藏",
          position: "bottom",
          duration: 500,
        });
        getPlaylistSubscribe({ t: 2, id:this.$route.query.id }).then(
          (data) => {
            console.log(data);
          }
        );
      } else {
        Toast({
          message: "收藏成功",
          position: "bottom",
          duration: 500,
        });
        getPlaylistSubscribe({ t: 1, id: this.$route.query.id }).then(
          (data) => {
            console.log(data);
          }
        );
      }
    },
   
  },
};
</script>

<style lang="less">
.topheader {
  position: relative;
  height: 270px;
  .img {
    width: 100%;
    height: 270px;
    position: relative;
    img {
      position: absolute;
      top: 0px;
      left: 0px;
      right: 0px;
      bottom: 0px;
      margin: auto;
      width: 100%;
    }
  }
  .title {
    font-size: 18px;
    position: absolute;
    left: 18px;
    bottom: 18px;
    color: #fff;
  }
  .button {
    font-size: 18px;
    position: absolute;
    left: 12px;
    top: 12px;
    color: #fff;
  }
}
.musicList {
  background-color: #fff;
  width: 100%;
  border-radius: 7px;
  position: relative;
  top: -7px;
  padding-bottom: 50px;
  dt {
    height: 36px;
    line-height: 36px;
    font-size: 16px;
    color: #454747;
    border-bottom: 1px solid #e3e3e3;
    position: relative;
    span {
      font-size: 14px;
      color: #797b7a;
    }
    .texticon{
      font-size: 25px;
      position: absolute;
      right: 16px;
     
    }
     .icon-shixin{
        color: red;
      }
    i {
      width: 44px;
      text-align: center;
      display: inline-block;
    }
  }
  dd {
    border-bottom: 1px solid #e3e3e3;
    height: 53px;
    display: flex;
    h1 {
      flex: 0 0 44px;
      line-height: 53px;
      text-align: center;
      font-size: 16px;
      color: #6d6e6e;
    }
    .info {
      flex: 1;
      h2 {
        font-size: 16px;
        color: #1b1c1c;
        margin-top: 10px;
        height: 22px;
        overflow: hidden;
      }
      h3 {
        font-size: 12px;
        color: #929393;
      }
    }
  }
}
</style>